<!doctype html>
<html lang="en">
<head>
    <title>Code coverage report for utils\parseCode.js</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="../prettify.css" />
    <link rel="stylesheet" href="../base.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type='text/css'>
        .coverage-summary .sorter {
            background-image: url(../sort-arrow-sprite.png);
        }
    </style>
</head>
<body>
<div class='wrapper'>
  <div class='pad1'>
    <h1>
      <a href="../index.html">all files</a> / <a href="index.html">utils/</a> parseCode.js
    </h1>
    <div class='clearfix'>
      <div class='fl pad1y space-right2'>
        <span class="strong">14.29% </span>
        <span class="quiet">Statements</span>
        <span class='fraction'>10/70</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Branches</span>
        <span class='fraction'>0/33</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Functions</span>
        <span class='fraction'>0/8</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">14.93% </span>
        <span class="quiet">Lines</span>
        <span class='fraction'>10/67</span>
      </div>
    </div>
  </div>
  <div class='status-line low'></div>
<pre><table class="coverage">
<tr><td class="line-count quiet">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">// 根据输入重复次数，输出相应字符串
const repeatN = (str, n = 0) =&gt; <span class="fstat-no" title="function not covered" >{</span>
    let res = <span class="cstat-no" title="statement not covered" >''</span>
<span class="cstat-no" title="statement not covered" >    while (n) {</span>
<span class="cstat-no" title="statement not covered" >        if (n % 2 === 1) { <span class="cstat-no" title="statement not covered" >res += str </span>}</span>
<span class="cstat-no" title="statement not covered" >        if (n &gt; 1) { <span class="cstat-no" title="statement not covered" >str += str </span>}</span>
<span class="cstat-no" title="statement not covered" >        n &gt;&gt;= 1</span>
    }
<span class="cstat-no" title="statement not covered" >    return res</span>
}
const genWrapTag = (str, tag, endFlage, text, n) =&gt; <span class="fstat-no" title="function not covered" >{</span>
    let blankNum = <span class="cstat-no" title="statement not covered" >repeatN('&amp;nbsp&amp;nbsp&amp;nbsp', n)</span>
    // 需要判断是否为
<span class="cstat-no" title="statement not covered" >    return text ? `&lt;${tag} class="hljs-tag"&gt;${blankNum}${str}&lt;/${tag}&gt;` :</span>
        `&lt;${tag} class="hljs-tag"&gt;${blankNum}&amp;lt${endFlage}${str}&amp;gt&lt;/${tag}&gt;`
}
&nbsp;
// 正则表达式,用于匹配格式化&lt;template&gt;代码
// 字符串空格
let regTagContent = /&lt;[^&gt;]+&gt;|(\S[^&lt;&gt;]+)/g // 非贪婪模式
let regGetTag = /(\w[\w-]+)\s*/
&nbsp;
const formatHtmlCode = str =&gt; <span class="fstat-no" title="function not covered" >{</span>
    // 用于字符串匹配栈
    let matchTags = <span class="cstat-no" title="statement not covered" >[]</span>
<span class="cstat-no" title="statement not covered" >    matchTags.push('-1')</span>
    // str = blankSpace(str);
    let parseRes = <span class="cstat-no" title="statement not covered" >''</span>
    let startTag
    let tag
    let endTagflag
<span class="cstat-no" title="statement not covered" >    str.replace(regTagContent, (s, m) =&gt; <span class="fstat-no" title="function not covered" >{</span></span>
<span class="cstat-no" title="statement not covered" >        s = s &amp;&amp; s.trim()</span>
<span class="cstat-no" title="statement not covered" >        m = m &amp;&amp; m.trim()</span>
&nbsp;
        // 如果v存在，说明为标签之间的文本匹配
<span class="cstat-no" title="statement not covered" >        if (m) {</span>
<span class="cstat-no" title="statement not covered" >            parseRes += genWrapTag(m, 'div', '', true, Math.max(matchTags.length - 1, 0))</span>
        } else <span class="cstat-no" title="statement not covered" >if (s) {</span>
            // 去掉&lt;&gt;
<span class="cstat-no" title="statement not covered" >            s = s.slice(1, -1)</span>
            // 判断是否为tag闭合，即判断是否含有/
<span class="cstat-no" title="statement not covered" >            endTagflag = /\//.test(s)</span>
<span class="cstat-no" title="statement not covered" >            startTag = matchTags.pop()</span>
<span class="cstat-no" title="statement not covered" >            tag = s.match(regGetTag)[1]</span>
<span class="cstat-no" title="statement not covered" >            if (endTagflag &amp;&amp; startTag === tag) {</span>
<span class="cstat-no" title="statement not covered" >                parseRes += genWrapTag(tag, 'div', '/', false, Math.max(matchTags.length - 1, 0))</span>
            } else {
<span class="cstat-no" title="statement not covered" >                matchTags.push(startTag)</span>
<span class="cstat-no" title="statement not covered" >                matchTags.push(tag)</span>
<span class="cstat-no" title="statement not covered" >                parseRes += genWrapTag(s, 'div', '', false, Math.max(matchTags.length - 2, 0))</span>
            }
        }
    })
<span class="cstat-no" title="statement not covered" >    if (matchTags.length &gt; 1) {</span>
<span class="cstat-no" title="statement not covered" >        console.error('输入模板不匹配！', matchTags)</span>
<span class="cstat-no" title="statement not covered" >        return ''</span>
    }
<span class="cstat-no" title="statement not covered" >    return parseRes</span>
}
// 正则表达式,用于匹配格式化&lt;script&gt;&lt;style&gt;代码
let regContent = /.+/g
const genWrapSptAndStyTag = (str, tag, n) =&gt; <span class="fstat-no" title="function not covered" >{</span>
    let blankNum = <span class="cstat-no" title="statement not covered" >repeatN('&amp;nbsp&amp;nbsp&amp;nbsp', n)</span>
    // 需要判断是否为
<span class="cstat-no" title="statement not covered" >    return `&lt;${tag} class="hljs-tag"&gt;${blankNum}${str}&lt;/${tag}&gt;`</span>
}
const formatSptAndStyCode = (str) =&gt; <span class="fstat-no" title="function not covered" >{</span>
    // 用于字符串匹配栈
    let matchTags = <span class="cstat-no" title="statement not covered" >[]</span>
<span class="cstat-no" title="statement not covered" >    matchTags.push('-1')</span>
    let parseRes = <span class="cstat-no" title="statement not covered" >''</span>
    let startTag
    let tag
    let nextTag
    let preLen
    let len
    let blankNum
<span class="cstat-no" title="statement not covered" >    parseRes += genWrapSptAndStyTag('&amp;ltscript&amp;gt', 'div', 0)</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    str.replace(regContent, s =&gt; <span class="fstat-no" title="function not covered" >{</span></span>
        // 过滤掉空格
<span class="cstat-no" title="statement not covered" >        s = s.trim()</span>
        // 全局匹配
<span class="cstat-no" title="statement not covered" >        tag = s.match(/[{}]/g)</span>
<span class="cstat-no" title="statement not covered" >        blankNum = matchTags.length</span>
<span class="cstat-no" title="statement not covered" >        if (tag) {</span>
<span class="cstat-no" title="statement not covered" >            preLen = len = tag.length</span>
<span class="cstat-no" title="statement not covered" >            while (len &gt; 0) {</span>
<span class="cstat-no" title="statement not covered" >                nextTag = tag.shift()</span>
<span class="cstat-no" title="statement not covered" >                startTag = matchTags.pop()</span>
<span class="cstat-no" title="statement not covered" >                if (startTag !== '{' || nextTag !== '}') {</span>
<span class="cstat-no" title="statement not covered" >                    matchTags.push(startTag)</span>
<span class="cstat-no" title="statement not covered" >                    matchTags.push(nextTag)</span>
                }
<span class="cstat-no" title="statement not covered" >                len--</span>
            }
        }
        // 暂时解决{}空格不匹配的问题
<span class="cstat-no" title="statement not covered" >        if (preLen === 1 &amp;&amp; nextTag === '}') { <span class="cstat-no" title="statement not covered" >blankNum -= 1 </span>}</span>
<span class="cstat-no" title="statement not covered" >        parseRes += genWrapSptAndStyTag(s, 'div', blankNum)</span>
    })
<span class="cstat-no" title="statement not covered" >    if (matchTags.length &gt; 1) {</span>
<span class="cstat-no" title="statement not covered" >        console.error('输入模板不匹配！', matchTags)</span>
<span class="cstat-no" title="statement not covered" >        return ''</span>
    }
<span class="cstat-no" title="statement not covered" >    parseRes += genWrapSptAndStyTag('&amp;lt/script&amp;gt', 'div', 0)</span>
<span class="cstat-no" title="statement not covered" >    return parseRes</span>
}
// 包装输出
const matchMethods = {
    formatHtmlCode: formatHtmlCode,
    formatSptAndStyCode: formatSptAndStyCode
}
const formatCode = (matchType, str) =&gt; <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >    return matchMethods[matchType](str)</span>
}
export default formatCode
&nbsp;</pre></td></tr>
</table></pre>
<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
  Code coverage
  generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Sun Feb 24 2019 15:08:50 GMT+0800 (中国标准时间)
</div>
</div>
<script src="../prettify.js"></script>
<script>
window.onload = function () {
        if (typeof prettyPrint === 'function') {
            prettyPrint();
        }
};
</script>
<script src="../sorter.js"></script>
</body>
</html>
